<template>
    <div class="loginbBody">
      <div class="loginbBody">
        <div class="loginDiv" v-if="isregister">
          <div class="login-content">
            <h1 class="login-title">用户注册</h1>
            <el-form :model="loginForm" label-width="100px"
                     :rules="rules" ref="loginForm">
              <el-form-item label="地点" prop="name">
                <el-input style="width: 200px" type="text" v-model="loginForm.name"
                          autocomplete="off" size="small"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input style="width: 200px" type="password" v-model="loginForm.password"
                          show-password autocomplete="off" size="small"></el-input>
              </el-form-item>
              <el-form-item label="确认密码" prop="password">
                <el-input style="width: 200px" type="password" v-model="loginForm.password2"
                          show-password autocomplete="off" size="small"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="registerUser" style="background-color: #0a81fa" >注 册</el-button>
                <el-button type="primary" @click="isregister=false" style="margin-left: 30px;background-color: #0a81fa">去登录</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
        <div class="loginDiv" v-else>
          <div class="login-content">
            <h1 class="login-title">用户登录</h1>
            <el-form :model="loginForm" label-width="100px"
                     :rules="rules" ref="loginForm">
              <el-form-item label="地点" prop="name">
                <el-input style="width: 200px" type="text" v-model="loginForm.name"
                          autocomplete="off" size="small"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input style="width: 200px" type="password" v-model="loginForm.password"
                          show-password autocomplete="off" size="small"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button style="background-color: #0a81fa" type="primary" @click="confirm">登 录</el-button>
                <el-button style="background-color: #0a81fa;margin-left: 30px" type="primary" @click="isregister=true">去注册</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
</template>

<script>
import axios from "axios";
export default {
  name: "login",
  data(){
    return{
      isLogin:true,
      isregister:false,
      isAdmin:false,
      loginForm:{
        name:'',
        password:'',
        password2:'',
      },
      rules:{
        name: [
          { required: true, message: '请输入地点', trigger: 'blur' },
          { min: 3, max: 15, message: '地点长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
        ]
      },
      count:0,
    }
  },
  created() {
    var user=JSON.parse(sessionStorage.getItem('user'));
    if (user && user.username.length>0) {
      if (user.username==='admin')
        this.isAdmin=true;
      this.isLogin=false;
    }
    this.getcount();
  },
  methods: {
    tologin() {
      sessionStorage.clear();
      this.isAdmin = false;
      this.isLogin = true;
    },
    tomine() {
      this.isAdmin = false;
    },
    registerUser() {
      // 验证地点长度
      if (this.loginForm.name.length < 3 || this.loginForm.name.length > 15) {
        this.$message.warning('地点长度应在3到15个字符之间');
        return '';
      }
      // 验证密码长度
      if (this.loginForm.password.length < 6) {
        this.$message.warning('密码长度不能少于6位');
        return '';
      }
      // 验证两次密码是否一致
      if (this.loginForm.password !== this.loginForm.password2) {
        this.$message.warning('两次密码不一致');
        return '';
      } else {
        axios.post('http://127.0.0.1:5003/register', {
          username: this.loginForm.name,
          password: this.loginForm.password
        }).then(res => {
          console.log(res);
          this.$message.success('注册成功,欢迎您:' + this.loginForm.name);
        }).catch(error => {
          this.$message.warning('注册失败,请联系管理人员');
        });
      }
    },
    confirm() {
      axios.post('http://127.0.0.1:5003/login', {
        username: this.loginForm.name,
        password: this.loginForm.password
      }).then(res => {
        console.log(res);
        this.isAdmin = res.data.username === 'admin';
        this.isLogin = false;
        sessionStorage.setItem('user', JSON.stringify(res.data));
        this.$message.success('登陆成功,欢迎您:' + res.data.username);
        this.$router.push('/');
      }).catch(error => {
        this.$message.warning('地点或密码错误')
      });
    },
    getcount() {
      axios.get('http://127.0.0.1:5003/users/count').then(res => {
        this.count = res.data.count;
      })
    }
  }
}
</script>

<style scoped >
    .loginbBody {
        width: 100%;
        height: 100%;
        background-color: #B3C0D1;
    }
    .loginDiv {
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -200px;
        margin-left: -250px;
        width: 450px;
        height: 350px;
        background: #fff;
        border-radius: 5%;

    }
    .login-title {
        margin: 20px 0;
        text-align: center;
    }
    .login-content {
        width: 400px;
        height: 250px;
        position: absolute;
        top: 25px;
        left: 25px;
    }
</style>
